<?php

namespace addons\oil\library\traits\model\order;

use addons\oil\model\Order;
use think\Cache;

trait OrderScope
{
    // 已失效
    public function scopeInvalid($query)
    {
        return $query->where('status', Order::STATUS_INVALID);
    }

    // 已取消
    public function scopeCancel($query)
    {
        return $query->where('status', Order::STATUS_CANCEL);
    }

    // 已转赠
    public function scopeShare($query)
    {
        return $query->where('status', Order::STATUS_SHARE);
    }

    // 未支付
    public function scopeNopay($query)
    {
        return $query->where('status', Order::STATUS_NOPAY);
    }

    // 已支付
    public function scopePayed($query)
    {
        return $query->where('status', 'in', [Order::STATUS_PAYED]);
//        return $query->where('status', 'in', [Order::STATUS_PAYED, Order::STATUS_FINISH]);
    }

    // 待收货
    public function scopeNoget($query)
    {
        $where = [
            'status' => Order::STATUS_PAYED,
            'refund_status' => ['not in', [Order::REFUND_STATUS_OK, Order::REFUND_STATUS_FINISH]]       // 没有退款完成
        ];
        return $query->where($where);
    }

    // 已完成
    public function scopeFinish($query)
    {
        return $query->where('status', Order::STATUS_FINISH);
    }

    // 已支付/已完成
    public function scopePayedOrFinish($query)
    {
        return $query->where('status', 'in', [Order::STATUS_PAYED, Order::STATUS_FINISH]);
    }
    public function scopeCanAftersale($query)
    {
//        return $query->where('status', 'in', [Order::STATUS_PAYED, Order::STATUS_FINISH]);
        //该项目完成加油的不能申请退款，申请过退款的不能再申请
//        return $query->where('status', 'in', [Order::STATUS_PAYED])->where('refund_status',Order::REFUND_STATUS_NOREFUND);
        //20240514 已核销的订单不能申请售后
        return $query->where('status', 'in', [Order::STATUS_PAYED])->where('refund_status',Order::REFUND_STATUS_NOREFUND)->where('hexiao_status',Order::HEXIAO_STATUS_NO);
    }
    //售后中订单
    public function scopeAftersale($query)
    {
        //该项目完成加油的不能申请退款，申请过退款的不能再申请
//        return $query->where('status', 'in', [Order::STATUS_PAYED])->where('refund_status','<>',Order::REFUND_STATUS_NOREFUND);
        // 20240527-修改 拒绝退款的订单显示在 已支付-已完成的列表里
        return $query->where('status', 'in', [Order::STATUS_PAYED])->where('refund_status','in',[Order::REFUND_STATUS_ING,Order::REFUND_STATUS_OK,Order::REFUND_STATUS_FINISH]);

    }
    // 没有售后的订单
    public function scopeNoAftersale($query)
    {
        // 20240527-修改 拒绝退款的订单显示在 已支付-已完成的列表里
//        return $query->where('refund_status',Order::REFUND_STATUS_NOREFUND);
        return $query->where('refund_status','in',[Order::REFUND_STATUS_NOREFUND,Order::REFUND_STATUS_REFUSE]);
    }
    public function scopeCanDelete($query)
    {
        return $query->where('status', 'in', [
            Order::STATUS_CANCEL, 
            Order::STATUS_INVALID,
            Order::STATUS_FINISH
        ]);
    }

    // 退款 (即将废弃，有专门的售后单列表)
    public function scopeRefundStatus($query)
    {
        $where = [
            'refund_status' => ['<>', Order::REFUND_STATUS_NOREFUND],       // 只要申请过退款
        ];

        return $query->where($where);
    }

    // 可分享转赠的订单
    public function scopeCanShare($query)
    {
        //是自己买的订单
        return $query->where('buyorgive',0)->where('status', 'in', [Order::STATUS_PAYED])->where('refund_status',Order::REFUND_STATUS_NOREFUND)->where('hexiao_status',Order::HEXIAO_STATUS_NO);
    }
    // 可领取转赠的订单
    public function scopeCanReceive($query)
    {
        //是自己买的订单
        return $query->where('buyorgive',0)->where('is_received',0)->where('status', 'in', [Order::STATUS_SHARE])->where('refund_status',Order::REFUND_STATUS_NOREFUND)->where('hexiao_status',Order::HEXIAO_STATUS_NO);
    }

    // 门店订单 scope

}
